<template>
	<xn-form-container
		:title="formData.id ? '编辑会员认证' : '增加会员认证'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="电话号码：" name="account">
				<a-input v-model:value="formData.account" placeholder="请输入电话号码" allow-clear />
			</a-form-item>
			<a-form-item label="姓名：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入姓名" allow-clear />
			</a-form-item>
			<a-form-item label="证件类型：" name="idCardType">
				<a-select v-model:value="formData.idCardType" placeholder="请选择证件类型" :options="idCardTypeOptions" />
			</a-form-item>
			<a-form-item label="证件号码：" name="idCardNumber">
				<a-input v-model:value="formData.idCardNumber" placeholder="请输入证件号码" allow-clear />
			</a-form-item>
			<a-form-item label="机构id：" name="orgId">
				<a-select v-model:value="formData.orgId" placeholder="请选择机构（支持搜索）" :options="orgIdOptions" />
			</a-form-item>
			<a-form-item name="phoneValidCode">
				<a-row :gutter="8">
					<a-col :span="17">
						<a-input v-model:value="formData.phoneValidCode" :placeholder="$t('login.smsCodePlaceholder')" size="large">
							<template #prefix>
								<mail-outlined class="text-black text-opacity-25" />
							</template>
						</a-input>
					</a-col>
					<a-col :span="7">
						<a-button size="large" class="xn-wd" @click="getPhoneValidCode" :disabled="state.smsSendBtn">
							{{ (!state.smsSendBtn && $t('login.getSmsCode')) || state.time + ' s' }}
						</a-button>
					</a-col>
				</a-row>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
	<a-modal
		v-model:open="visible"
		:width="400"
		:title="$t('login.machineValidation')"
		@cancel="handleCancel"
		@ok="handleOk"
	>
		<a-form ref="phoneLoginFormModalRef" :model="phoneFormModalData" :rules="formModalRules">
			<a-form-item name="validCode">
				<a-row :gutter="8">
					<a-col :span="17">
						<a-input
							v-model:value="phoneFormModalData.validCode"
							:placeholder="$t('login.validLaceholder')"
							size="large"
						>
							<template #prefix>
								<verified-outlined class="text-black text-opacity-25" />
							</template>
						</a-input>
					</a-col>
					<a-col :span="7">
						<img :src="validCodeBase64" class="xn-findform-line" @click="getPhonePicCaptcha" alt="" />
					</a-col>
				</a-row>
			</a-form-item>
		</a-form>
	</a-modal>
</template>

<script setup name="bizMemberAuthForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizMemberAuthApi from '@/api/biz/bizMemberAuthApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const idCardTypeOptions = ref([])
	const orgIdOptions = ref([])
	// 机器验证码判断条件
	const phoneLoginFormRef = ref()

	// 验证码状态
	let state = ref({
		time: 60,
		smsSendBtn: false
	})
	// 验证码标识
	const phoneValidCodeReqNo = ref('')

	// 点击获取短信验证码
	const getPhoneValidCode = () => {
		formRules.value.account = [required('请输入11位手机号'), rules.phone]
		formRules.value.phoneValidCode = [required('请输入短信验证码'), rules.number]
		delete formRules.value.phoneValidCode
		phoneLoginFormRef.value.validate().then(() => {
			// 显示弹框
			visible.value = true
			// 获取内部图片验证码
			getPhonePicCaptcha()
		})
	}
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		idCardTypeOptions.value = tool.dictList('IDCARD_TYPE')
		orgIdOptions.value = tool.dictList('ORG_CATEGORY')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRules.value.account = [required('请输入11位手机号'), rules.account]
		formRules.value.phoneValidCode = [required('请输入短信验证码'), rules.number]
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizMemberAuthApi
					.bizMemberAuthSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
